import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vueJsx from '@vitejs/plugin-vue-jsx';

// https://vitejs.dev/config/
// mode 模式
export default defineConfig(({ mode }) => {
  // 以mode中的模式，进行加载.env文件
  // eslint-disable-next-line no-undef
  const env = loadEnv(mode, process.cwd())

  const port = Number(env.VITE_PORT)
  // const open = env.VITE_OPEN
  console.log(port)

  return {
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      // open: open,
      // port: port
      proxy: {
        '/api': {
          target: 'http://124.221.74.149:3000/',
          // target: 'http://172.20.10.2:3000/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }
  }
})
